using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using BSCore;

namespace BSEntities
{



    public class BSEntity :BSCore.MapEntity
    {

        //VARIABLES


      //  private float m_TileRange = 0F;        //attack range in terms of tiles
       // private float m_Damage = 0f;       //Damage dealt per hit
       // private float m_DamageTileRadius = 0f; //the damage radius 0 for 1 tile 1 for a square of 9 tiles etc 
      //  private float m_PixelMoveRate = 0f;    //movement rate of entity
     //   private float m_LayerRange = 0f;       //attack range in terms of layers (storeys) 
        private float m_CurrentLayer = 0;      
        private float m_LayerSpan = 1;    
        private bool m_IsBuilding = false;


        //PROPERTIES
        public Skills Skills { get; set; }

        public bool Targetable { get; set; }

        public float Group { get; set; } //Alternative to Hostility enum, this way we can have multiple groups that can be against each other

        public float Layer { get; set; }//denotes which layer the entity is currently in

        public float LayerSpan { get; set; } //denotes if the entity is spanning more than 1 layer (structures can be multi layer)

        public bool IsAlive { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }




        //METHODS FOLLOW HERE


        /// <summary>
        /// Basic constructor
        /// </summary>
        public BSEntity(BSGame y_Game)
            : base(y_Game)
        {
                
        }
        /// <summary>
        /// Define a new entity
        /// </summary>
        /// <param name="y_HP">original Hitpoints</param>
        /// <param name="y_CurrentHP">Current Hitpoints</param>
        /// <param name="y_TileRange">Attack range in Tiles</param>
        /// <param name="y_Damage">Damage Value</param>
        /// <param name="y_DamageTileRadius">Damage radius in terms of tiles</param>
        /// <param name="y_PixelMoveRate">speed of entity</param>
        /// <param name="y_LayerRange">span of attack in terms of layers ie hieght </param>
        /// <param name="y_isFriend">define if it is a friend (true) or Foe (false)</param>
        /// <param name="y_CurrentLayer">define </param>
        /// <param name="y_LayerSpan">define if entity spans across layers</param>
        /// <param name="y_IsBuilding">true if this is a structure </param>
        public BSEntity(BSGame y_Game,
                          string y_Name,
                          float y_HP,       
                          float y_CurrentHP,
                          float y_TileRange ,
                          float y_Damage ,
                          float y_DamageTileRadius ,
                          float y_PixelMoveRate,  
                          float y_LayerRange, 
                          Hostility y_Hostility, 
                          float y_CurrentLayer,
                          float y_LayerSpan,
                          bool y_IsBuilding                    ):base(y_Game)
        {
            Name = y_Name;
            //m_Damage =  y_Damage ;
            //m_DamageTileRadius =  y_DamageTileRadius ;
            //m_PixelMoveRate = y_PixelMoveRate;
         //   m_LayerRange =  y_LayerRange;
            m_CurrentLayer =  y_CurrentLayer;
            m_LayerSpan =  y_LayerSpan;
            m_IsBuilding = y_IsBuilding;
            
        }

        
        /// <summary>
        /// returns one of the nearest targets available
        /// </summary>
        /// <returns>a mapentity</returns>
        public MapEntity GetTarget()
        {
            MapEntities EntityList = new MapEntities();
            MapEntity Target = new BSEntity((BSGame) Game);

            //switch (m_Hostility)
            //{
            //    case Hostility.Foe:
            //        Targets = Hostility.Friend;
            //        break;
            //    case Hostility.Friend:
            //        Targets = Hostility.Foe;
            //        break;
            //    default:
            //        Targets = Hostility.All;
            //        break;

            //}

            float layerRange = Skills.GetSkillByName("FieldOfViewDepth").EffectiveSkill;
            for (float j = 0; j == Skills.GetSkillByName("FieldOfView").EffectiveSkill; j++)
            {
                float xPos1 = MathHelper.Clamp(this.Position.X - j, 0f, ((BSGame)Game).Map.Size.X);
                float yPos1 = MathHelper.Clamp(this.Position.Y - j, 0f, ((BSGame)Game).Map.Size.Y); ;
                float zPos1 = MathHelper.Clamp(this.Position.Z - layerRange, 0f, 9f);

                float xPos2 = MathHelper.Clamp(this.Position.X + j, 0f, ((BSGame)Game).Map.Size.X) ;
                float yPos2 = MathHelper.Clamp(this.Position.Y + j, 0f, ((BSGame)Game).Map.Size.Y); ;
                float zPos2 = MathHelper.Clamp(this.Position.Z + layerRange, 0f, 9f);
  
                Vector3 FromVector = new Vector3(xPos1, yPos1, zPos1);
                Vector3 ToVector = new Vector3(xPos2, yPos2, zPos2);

                MapEntities tmpEntityList = new MapEntities();
                 
                tmpEntityList = ((BSGame)Game).Map.MapEntities.GetEntitiesByRange(FromVector, ToVector);
                //if there is an eligible target quit looping and get ready to shoot
                if (tmpEntityList.Count > 0)
                {
                    break;
                }


                foreach (BSEntity entity in tmpEntityList)
                {
                    if (entity.Group != Group)
                    {
                        EntityList.Add(entity);
                    }
                }

            }
            Random r = new Random();
            Target = EntityList.ElementAt(r.Next(0, EntityList.Count()));
            return Target;
 
        }







        

       
    }
}
